package br.com.estevam.gallery.bancoimagens.visao;

import br.com.estevam.gallery.data.ImagemData;

import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.jdesktop.layout.GroupLayout;
import org.jdesktop.layout.LayoutStyle;

public class BuscaBanco extends JDialog {

	private static final long serialVersionUID = 8776303067405204324L;

	private JButton abrir;
	private JButton cancelar;
	private JButton jButton3;
	private JLabel jLabel1;
	private JScrollPane jScrollPane1;
	private JList nomes;
	private JTextField palavra;

	public BuscaBanco(Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
		setLocationRelativeTo(parent);
	}

	private ActionSupport actionSupport = new ActionSupport();

	public void addActionListener(ActionListener listener) {
		actionSupport.addActionListener(listener);
	}

	public void removeActionListener(ActionListener listener) {
		actionSupport.removeActionListener(listener);
	}

	private void initComponents() {
		jLabel1 = new JLabel();
		jScrollPane1 = new JScrollPane();
		nomes = new JList();
		palavra = new JTextField();
		cancelar = new JButton();
		abrir = new JButton();
		jButton3 = new JButton();

		FormListener formListener = new FormListener();

		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setTitle("Buscar imagens no banco");
		jLabel1.setText("Palavra-chave para a busca:");

		nomes.addListSelectionListener(formListener);

		jScrollPane1.setViewportView(nomes);

		cancelar.setText("Cancelar");
		cancelar.setActionCommand("cancelar");
		cancelar.addActionListener(formListener);

		abrir.setText("Abrir");
		abrir.setActionCommand("abrir");
		abrir.setEnabled(false);
		abrir.addActionListener(formListener);

		jButton3.setText("...");
		jButton3.setActionCommand("procurar");
		jButton3.addActionListener(formListener);

		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(
				layout.createParallelGroup(GroupLayout.LEADING)
				.add(layout.createSequentialGroup()
						.addContainerGap()
						.add(layout.createParallelGroup(GroupLayout.LEADING)
								.add(jScrollPane1, GroupLayout.DEFAULT_SIZE, 441, Short.MAX_VALUE)
								.add(GroupLayout.TRAILING, layout.createSequentialGroup()
										.add(abrir)
										.addPreferredGap(LayoutStyle.RELATED)
										.add(cancelar))
										.add(layout.createSequentialGroup()
												.add(jLabel1)
												.addPreferredGap(LayoutStyle.RELATED)
												.add(palavra, GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
												.addPreferredGap(LayoutStyle.RELATED)
												.add(jButton3)))
												.addContainerGap())
		);

		layout.linkSize(new Component[] {abrir, cancelar}, GroupLayout.HORIZONTAL);

		layout.setVerticalGroup(
				layout.createParallelGroup(GroupLayout.LEADING)
				.add(layout.createSequentialGroup()
						.addContainerGap()
						.add(layout.createParallelGroup(GroupLayout.BASELINE)
								.add(jLabel1)
								.add(jButton3)
								.add(palavra, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(LayoutStyle.RELATED)
								.add(jScrollPane1, GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
								.addPreferredGap(LayoutStyle.RELATED)
								.add(layout.createParallelGroup(GroupLayout.BASELINE)
										.add(cancelar)
										.add(abrir))
										.addContainerGap())
		);
		pack();
	}

	private class FormListener implements ActionListener, ListSelectionListener {
		public void actionPerformed(ActionEvent evt) {
			if (evt.getSource() == cancelar) {
				BuscaBanco.this.cancelarActionPerformed(evt);
			}
			else if (evt.getSource() == abrir) {
				BuscaBanco.this.abrirActionPerformed(evt);
			}
			else if (evt.getSource() == jButton3) {
				BuscaBanco.this.jButton3ActionPerformed(evt);
			}
		}

		public void valueChanged(ListSelectionEvent evt) {
			if (evt.getSource() == nomes) {
				BuscaBanco.this.nomesValueChanged(evt);
			}
		}
	}

	private void nomesValueChanged(ListSelectionEvent evt) {
		abrir.setEnabled(true);
	}

	private void jButton3ActionPerformed(ActionEvent evt) {
		actionSupport.fireActionEvent(evt);        
	}

	private void abrirActionPerformed(ActionEvent evt) {
		dispose();
		actionSupport.fireActionEvent(evt);
	}

	private void cancelarActionPerformed(ActionEvent evt) {
		dispose();
	}

	public void setDadosImagens(List<ImagemData> dadosImagens) {
		DefaultListModel modelo = new DefaultListModel();
		modelo.clear();
		for (ImagemData dadosImagem : dadosImagens) {
			modelo.addElement(dadosImagem);
		}
		nomes.setModel(modelo);
		abrir.setEnabled(false);
	}

	public String getPalavra() {
		return palavra.getText();
	}

	public ImagemData getDadosImagemSelecionada() {
		return (ImagemData) nomes.getSelectedValue();
	}

	public static void main(String args[]) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				new BuscaBanco(new JFrame(), true).setVisible(true);
			}
		});
	}
}
